/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.fetcher.gui.action;

import com.torunski.crawler.link.Link;
import com.torunski.crawler.parser.PageData;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import pl.tom.social.common.sitetool.ISiteTools;
import pl.tom.social.common.sitetool.SiteToolsFactory;
import pl.tom.social.common.types.PageType;
import pl.tom.social.dal.SocialDatabaseFacade;
import pl.tom.social.dal.entity.Site;
import pl.tom.social.dal.entity.SiteEntry;
import pl.tom.social.fetcher.crawler.SiteParserEventListener;
import pl.tom.social.fetcher.crawler.SocialHttpClientParser;

public final class NewLinks implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		SocialDatabaseFacade db = new SocialDatabaseFacade();
		List<Site> sites = db.getAllSites();
		for (Site site : sites) {
		//Site site = sites.get(45);
			SiteParserEventListener s = new SiteParserEventListener(site);
			List<SiteEntry> entries = db.getSiteEntries(site);

			SocialHttpClientParser parser = new SocialHttpClientParser();
			PageData data = parser.load(new Link(site.getUrl()));
			String page = (String) data.getData();
			ISiteTools tools = SiteToolsFactory.getTools(site);
			if(page != null)
				s.parseLinks(db, site.getUrl(), tools.getBlogEntryBody(page, PageType.Home), site.getAdded());

			for (SiteEntry entry : entries) {
				s.parseLinks(db, entry.getUrl(), entry.getBody(), entry.getAdded());
			}
		}
	}
}
